package com.itheima.restkeeper.face;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.restkeeper.UserFace;
import com.itheima.restkeeper.pojo.Role;
import com.itheima.restkeeper.pojo.User;
import com.itheima.restkeeper.req.UserVo;
import com.itheima.restkeeper.service.IUserAdapterService;
import com.itheima.restkeeper.service.IUserService;
import com.itheima.restkeeper.utils.BeanConv;
import com.itheima.restkeeper.utils.EmptyUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboService;
import org.apache.dubbo.config.annotation.Method;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.ArrayList;
import java.util.List;

/**
 * @ClassName UserFaceImpl.java
 * @Description TODO
 */
@Slf4j
@DubboService(version = "${dubbo.application.version}",timeout = 5000,
        methods ={
                @Method(name = "findUserVoPage",retries = 2),
                @Method(name = "createUser",retries = 0),
                @Method(name = "updateUser",retries = 0),
                @Method(name = "deleteUser",retries = 0)
        })
public class UserFaceImpl implements UserFace {

    @Autowired
    IUserService userService;

    @Autowired
    IUserAdapterService userAdapterService;

    @Override
    public Page<UserVo> findUserVoPage(UserVo userVo, int pageNum, int pageSize) {
        //查询Page<User>用户分页
        Page<User> userVoPage = userService.findUserVoPage(userVo, pageNum, pageSize);
        //转化Page<User>为Page<UserVo>
        Page<UserVo> pageVo = new Page<>();
       BeanConv.toBean(userVoPage, pageVo);

        //转换List<User>为 List<UserVo>
        List<User> pageRecords = userVoPage.getRecords();
        List<UserVo> userVosList = BeanConv.toBeanList(pageRecords, UserVo.class);
        //查询角色信息
        if (!EmptyUtil.isNullOrEmpty(pageRecords)){
            userVosList.forEach(n->{
                List<Role> roleList = userAdapterService.findRoleByUserId(n.getId());
                ArrayList<String> arrayList = new ArrayList<>();
                for (Role role : roleList) {
                    arrayList.add(String.valueOf(role.getId()));
                }
                String[] roleIdS = new String[arrayList.size()];
                arrayList.toArray(roleIdS);
                n.setHasRoleIds(roleIdS);
            });
        }
        pageVo.setRecords(userVosList);
        //返回结果
        return pageVo;
    }

    /**
     * 创建用户
     * @param userVo 对象信息
     * @return
     */
    @Override
    public UserVo createUser(UserVo userVo) {

        return BeanConv.toBean(userService.createUser(userVo),UserVo.class);
    }

    @Override
    public Boolean updateUser(UserVo userVo) {

        return userService.updateUser(userVo);
    }

    @Override
    public Boolean deleteUser(String[] checkedIds) {
        return userService.deleteUser(checkedIds);
    }

    @Override
    public UserVo findUserByUserId(Long userId) {
        return BeanConv.toBean(userService.getById(userId),UserVo.class);
    }

    @Override
    public List<UserVo> findUserVoList() {
        return BeanConv.toBeanList(userService.findUserVoList(),UserVo.class);
    }
}
